<?php
if (!defined('ZAZA')) exit('No direct script access allowed');
/**
 * @copyright     ZaZa Group
 * @author        Sir Doan
 */

System::registerFile('dwooAutoload', PLUGIN_PATH . DS, 'dwoo'. DS);
class Template extends Dwoo
{
    protected $dwoo_data = array();
    public $layout = 'default';
    /**
     * Constructor for the DwooTemplate engine 
     */
    public function __construct() {
        // Call parents constructor
        parent::__construct();

        // Set the config settings
        $this->initialize();  

        // Assign some defaults to dwoo       
        $this->dwoo_data            = new Dwoo_Data();
        $this->dwoo_data->js_files  = array();
        $this->dwoo_data->css_files = array();
        //$this->dwoo_data->CI        = $CI;
        //$this->dwoo_data->site_url  = $CI->config->site_url(); // so we can get the full path to CI easily
        $this->dwoo_data->uniqid    = uniqid();
        $this->dwoo_data->timestamp = mktime();

        //log_message('debug', "Dwoo Template Class Initialized");
    }  

    /**
     * Assign data to dwoo data object
     *
     * @param string $key
     * @param mixed $value
     */
    public function assign($key, $value) {
        $this->dwoo_data->$key = $value;
    }      

    /**
     * Add Javascript files to template
     *
     * @param string $js
     */
    public function add_js($js) {
        $current   = $this->dwoo_data->js_files;
        $current[] = $js;
        $this->dwoo_data->js_files = $current;
    }     

    /**
     * Add Css stylesheets to template
     *
     * @param string $css
     */
    public function add_css($css) {
        $current   = $this->dwoo_data->css_files;
        $current[] = $css;
        $this->dwoo_data->css_files = $current;
    }

    /**
     * Display or return the compiled template
     * Since we assign the results to the standard CI output module
     * you can also use the helper from CI in your templates!!
     *
     * @param string $sTemplate
     * @param boolean $return
     * @return mixed
     */
    public function display($sTemplate) {  
        // Check temp admin
        if(Module::$current->data['type'] == 1) $path = 'admin'. DS;
        else $path = '';
        
        $tempDir = $this->template_dir. $path . $sTemplate . TEMP;
        
        // Check temp
        if( !file_exists($tempDir)) {
            $message = sprintf('Template file \'%s\' not found.', $sTemplate . TEMP);   
        }
                         
        // Create new template
        $tpl = new Dwoo_Template_File($tempDir);
                                    
        // render the template
        $template = $this->get($tpl, $this->dwoo_data); 
        
        return self::_layout($template); 
    } 
    
    private function _layout($html = '')
    {
        // Set Layout
        $layoutDir = $this->layout_dir . $this->layout . TEMP;  
        // Check layout
        if( !file_exists($layoutDir)) {
            $message = sprintf('Template file \'%s\' not found.', $this->layout . TEMP);   
        }     
        // Create new layout
        $layout = new Dwoo_Template_File($layoutDir);
        // render the layput
        $content_for_layout = 'content_for_layout';
        $this->dwoo_data->$content_for_layout = $html;
        return $this->get($layout, $this->dwoo_data);
          
    } 
    
    public function widget($sTemplate = null)
    {
        $tempDir = $this->template_dir. 'widgets'. DS . $sTemplate . TEMP;
        
        // Check temp
        if( !file_exists($tempDir)) {
            $message = sprintf('Template file \'%s\' not found.', $sTemplate . TEMP);   
        }
                         
        // Create new template
        $tpl = new Dwoo_Template_File($tempDir);
                                    
        // render the template
        return $this->get($tpl, $this->dwoo_data); 
    }

    /**
     * Assign the dwootemplate config items to the instance 
     */
    private function initialize() {  
        $config = require_once ROOT_PATH . DS .'config'. DS .'dwootemplate'. EXT;     
        foreach ($config as $key => $val) {
                $this->$key = $val;
        }
    }    
} 
?>
